package com.docker.sys.role;

import com.docker.core.mvc.service.BaseService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.convert.RecordKit;

import java.util.List;

/**
 * @Author ldl
 * @Create 2020/09/18 018 09:39
 */
public class RoleService extends BaseService {

    @Inject
    private RoleDao roleDao;

    public List<Record> list(Okv cond) {
        return roleDao.list(cond);
    }

    public boolean save(RoleBean bean, String userId) {
        if (roleDao.exist(bean.getDept_code(), bean.getRole_name(), "-1")) {
            throwError("角色名已存在");
        }
        bean.setRole_id(getUUID32());
        bean.setStatus_flag("1");
        bean.setCreate_time(getNowDateTime());
        bean.setCreate_oper_id(userId);
        bean.setUpdate_time(bean.getCreate_time());
        bean.setUpdate_oper_id(userId);
        return roleDao.save(RecordKit.objectToRecord(bean));
    }

    public boolean update(RoleBean bean, String userId) {
        Record role = roleDao.info(bean.getRole_id());
        if (!StrKit.notNull(role)) {
            throwError("角色不存在");
        }
        if (roleDao.exist(bean.getDept_code(), bean.getRole_name(), bean.getRole_id())) {
            throwError("角色名已存在");
        }
        role.set("role_name", bean.getRole_name());
        role.set("role_desc", bean.getRole_desc());
        role.set("role_order", bean.getRole_order());
        role.set("update_time", getNowDateTime());
        role.set("update_oper_id", userId);
        return roleDao.update(role);
    }

}
